﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MultiBoxer.Properties;

namespace MultiBoxer
{

    public class SettingsModel
    {
        public int Xpos { get; set; }
        public int Ypos { get; set; }

        private void updateKV(KeyValueConfigurationCollection kv, string key_name, string value_string)
        {
            kv.Remove(key_name);
            kv.Add(key_name, value_string);
        }


        public void SaveSettings()
        {
            System.Configuration.Configuration cfg = 
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            KeyValueConfigurationCollection kv = cfg.AppSettings.Settings;

            updateKV(kv, "Xpos", ""+Xpos);
            updateKV(kv, "Ypos", ""+Ypos);

            cfg.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public void LoadSettings()
        {
            System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader();

            Xpos = (int)getFloatSetting(reader, "Xpos", 0);
            Ypos = (int)getFloatSetting(reader, "Ypos", 0);

        }

        bool getBoolSetting(System.Configuration.AppSettingsReader reader, string valueName, bool default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(int));
                if ( o == null)
                    return default_value;
                return 0 != (int)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        byte getByteSetting(System.Configuration.AppSettingsReader reader, string valueName, byte default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(int));
                if (o == null)
                    return default_value;
                return (byte)(int)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        float getFloatSetting(System.Configuration.AppSettingsReader reader, string valueName, float default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(float));
                if (o == null)
                    return default_value;
                return (float)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }

        string getStringSetting(System.Configuration.AppSettingsReader reader, string valueName, string default_value)
        {
            try
            {
                Object o = reader.GetValue(valueName, typeof(string));
                if (o == null)
                    return default_value;
                return (string)o;
            }
            catch (InvalidOperationException)
            {
                return default_value;
            }
        }


    }
}
